package com.janu.mmchat;

import com.janu.mmchat.tool.UIHelper;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;


public class Activity_Main extends FragmentActivity{

	private TabHost mTabHost;
	private TabWidget mTabWidget;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initTab();
    }
    
    private LinearLayout mIndexLayout,mHealthyCheckLayout,mShoppingMallLayout,mShopingCartLayout,mIndividualcenterLayout;
    
    private void initView(){
    	
    	mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    	mTabWidget = (TabWidget)findViewById(android.R.id.tabs);
    	
    	mIndexLayout = createTab(R.drawable.main_tab_index_icon,R.string.main_tab_index);
    	mHealthyCheckLayout = createTab(R.drawable.main_tab_healthycheck_icon,R.string.main_tab_healthycheck);
    	mShoppingMallLayout = createTab(R.drawable.main_tab_shoppingmall_icon, R.string.main_tab_shoppingmall);
    	mShopingCartLayout = createTab(R.drawable.main_tab_shoppingcart_icon, R.string.main_tab_shoppingcart);
    	mIndividualcenterLayout = createTab(R.drawable.main_tab_individual_center_icon, R.string.main_tab_individualcenter);
    	
    }

    private static final String TAB_INDEX =  "index";
    private static final String TAB_HEALTHYCHECK =  "healthycheck";
    private static final String TAB_SHOPPINGMALL =  "shoppingmall";
    private static final String TAB_SHOPPINGCART =  "shoppingcart";
    private static final String TAB_INDIVIDUALCENTER =  "individualcenter";
    
    private void initTab(){
    	
    	mTabHost.setup();
    	
    	mTabHost.addTab(this.createTabSpec(TAB_INDEX, mIndexLayout, R.id.fragment_index));
    	mTabHost.addTab(this.createTabSpec(TAB_HEALTHYCHECK, mHealthyCheckLayout, R.id.fragment_healthycheck));
    	mTabHost.addTab(this.createTabSpec(TAB_SHOPPINGMALL, mShoppingMallLayout, R.id.fragment_shoppingmall));
    	mTabHost.addTab(this.createTabSpec(TAB_SHOPPINGCART, mShopingCartLayout, R.id.fragment_shoppingcart));
    	mTabHost.addTab(this.createTabSpec(TAB_INDIVIDUALCENTER, mIndividualcenterLayout, R.id.fragment_individualcenter));
    	
    	int count = mTabWidget.getChildCount();
    	int avgPx = UIHelper.getScreenWidth(this)/count;
    	for (int i = 0; i < count; i++) {
			View view = mTabWidget.getChildAt(i);
			view.getLayoutParams().width = avgPx;
			view.setBackgroundResource(R.drawable.selector_tab_background);
		}
    	
    }
    

	private LinearLayout createTab(int resId,int stringId){
    	LinearLayout layout = (LinearLayout)LayoutInflater.from(this)
    			.inflate(R.layout.tab_indicator, mTabWidget, false);
    	ImageView ivImage4 = (ImageView)layout.findViewById(R.id.tabImage);
    	ivImage4.setBackgroundResource(resId);
    	TextView tvText4 = (TextView)layout.findViewById(R.id.tabText);
    	tvText4.setText(stringId);
    	return layout;
    }
    
    private TabHost.TabSpec createTabSpec(String tag,LinearLayout layout,int resId){
    	TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag);
    	tabSpec.setIndicator(layout);
    	tabSpec.setContent(resId);
    	return tabSpec;
    }
    
    Handler mainHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			mTabHost.setCurrentTab(msg.what);
		}
    	
    };

}


	